package com.etoak.service;

import cn.hutool.json.JSONUtil;
import com.etoak.common.vo.Email;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailService {

    @Value("${spring.mail.username}")
    String senderAddress;

    @Autowired
    JavaMailSender javaMailSender;

    @RabbitListener(queues = "email")
    public void send(String msg) {
        System.out.println("收到消息" + msg);
        Email email = JSONUtil.toBean(msg, Email.class);
        SimpleMailMessage message = new SimpleMailMessage();
        //发件人
        message.setFrom(this.senderAddress);
        //收件人
        message.setTo(email.getReceiver());
        //抄送人
        message.setCc("2291246971@qq.com");
        //主题
        message.setSubject(email.getSubject());
        //正文
        message.setText(email.getContent());
        System.out.println("开始发送邮件");
        javaMailSender.send(message);
        System.out.println("发送结束");

    }

}
